package com.summer.reference;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

public class ReferenceTest {

	public static void main(String[] args) {
		A a = new A();
//		A aa = a;
//		A weakA = new WeakReference<A>(a).get();
//		A softA = new SoftReference<A>(a).get();
		WeakReference<A> weakA = new WeakReference<A>(a);
//		SoftReference<A> softA = new SoftReference<A>(a);
		A aa = weakA.get();
		
		int i = 0 ,b=0;
		while(true){
			i++;
			if(i>3)
				a=null;
			if(weakA.get() != null){
				
				System.out.println("weakA对象还存在"+i+" loops - "+weakA);
			}else{
				System.out.println("weakA对象不存在了.");
				break;
			}	
			
//			if(softA.get() != null){
//				b++;
//				System.out.println("softA对象还存在"+b+" loops - "+softA);
//			}else{
//				System.out.println("softA对象不存在了.");
//				break;
//			}
			
			System.gc();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		
	}
	
	public static class A{
		public String name ="A";
	}
}
